package com.jy.api.controller.admin;

import com.jy.api.dao.StoreDao;
import com.jy.api.domain.SMSType;
import com.jy.api.domain.Store;
import com.jy.api.service.VerificationCodeService;
import com.jy.api.util.Contens;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * Created by lihu on 2018/1/6.
 */
@Slf4j
@RestController
@RequestMapping(value = "v1/admin", produces = "application/json;charset=utf-8")
public class AdminSMSController {

    @Autowired
    VerificationCodeService verificationCodeService;

    @Autowired
    StoreDao storeDao;

    @Autowired
    StringRedisTemplate stringRedisTemplate;


    /**
     * 用于店主发送短信
     *
     * @param storeId
     * @return
     */
    @RequestMapping(value = "{storeId}/validate-code", method = RequestMethod.POST)
    public ResponseEntity verificationcode(@PathVariable String storeId,
                                           @RequestBody String verifyCode) {
        try {

            Store store = storeDao.findOne(storeId);
            if (store == null) {
                return new ResponseEntity("发送失败！店铺信息有误！", HttpStatus.BAD_REQUEST);
            }
            String key = Contens.SMS_KEY.replace("MOBILE", store.getUsername()).
                    replace("TYPE", SMSType.QR_CODE.toString());
            String code = stringRedisTemplate.opsForValue().get(key);

            if (StringUtils.isBlank(code) || !StringUtils.equals(code, verifyCode)) {
                return new ResponseEntity("校验失败！验证码错误！", HttpStatus.BAD_REQUEST);
            } else {
                return new ResponseEntity(HttpStatus.OK);
            }
        } catch (Exception ex) {
            log.error("storeSendSms is error:{}", ex);
            return new ResponseEntity("系统运行错误", HttpStatus.INTERNAL_SERVER_ERROR);
        }

    }


    /**
     * 用于店主发送短信
     *
     * @param storeId
     * @return
     */
    @RequestMapping(value = "{storeId}/verify-code", method = RequestMethod.POST)
    public ResponseEntity storeSendSms(@PathVariable String storeId) {
        try {
            Store store = storeDao.findOne(storeId);
            if (store == null) {
                return new ResponseEntity("发送失败！店铺信息有误！", HttpStatus.BAD_REQUEST);
            }
            String msg = verificationCodeService.setCode(store.getUsername(), SMSType.QR_CODE);
            if (StringUtils.isBlank(msg)) {
                return new ResponseEntity(HttpStatus.OK);
            } else {
                return new ResponseEntity(msg, HttpStatus.BAD_REQUEST);
            }
        } catch (Exception ex) {
            log.error("storeSendSms is error:{}", ex);
            return new ResponseEntity("系统运行错误", HttpStatus.INTERNAL_SERVER_ERROR);
        }

    }
}
